package com.elepro.shoppingmall.service.orders;


import com.elepro.shoppingmall.entity.Goods;
import com.elepro.shoppingmall.entity.orders.ShoppingCart;
import com.elepro.shoppingmall.mapper.goods.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApiShoppingCartServiceIm {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private GoodsMapper goodsMapper;


    /**
     * 获取缓存中的购物车信息
     * @return
     */
    public List<ShoppingCart> getShoppingCartList(Long userId){

        if (userId == null)
            return null;

        List<ShoppingCart> shoppingCartList = redisTemplate.opsForList().range("shoppingCartList"+userId,0,-1);

        if (shoppingCartList !=null)
            return shoppingCartList;

        return null;
    }

    public String insertShoppingCart(ShoppingCart shoppingCart){

        if (shoppingCart.getGoodsId() == null||shoppingCart.getUserId() == null)
            return "fail";
        Goods goods = goodsMapper.getGoodsById(shoppingCart.getGoodsId());
        shoppingCart.setGoodsName(goods.getGoodsName());
        shoppingCart.setPictureUrl(goods.getPicture());

        List<ShoppingCart> shoppingCartList = getShoppingCartList(shoppingCart.getUserId());

        String goodsType = goodsMapper.getGoodsTypeByTypeId(goods.getGoodsTypeId()).getGoodsType();

        shoppingCart.setGoodsAmount(1);
        shoppingCart.setMoneys(goods.getPrice());
        shoppingCart.setPrice(goods.getPrice());
        shoppingCart.setGoodsType(goodsType);
        if (shoppingCartList == null){

            redisTemplate.opsForList().leftPush("shoppingCartList"+shoppingCart.getUserId(),shoppingCart);

            return "success";
        }
        int i = 0 ;
        for (ShoppingCart shoppingCart1 : shoppingCartList){
            if (shoppingCart1.getGoodsId().equals(shoppingCart.getGoodsId())){
                shoppingCart1.setGoodsAmount(shoppingCart1.getGoodsAmount()+1);
                shoppingCart1.setMoneys(shoppingCart1.getMoneys()+goods.getPrice());
//                System.out.println(shoppingCart1.toString());
                redisTemplate.opsForList().set("shoppingCartList"+shoppingCart.getUserId(),i,shoppingCart1);
                return "success";
            }
            i++;
        }
        redisTemplate.opsForList().leftPush("shoppingCartList"+shoppingCart.getUserId(),shoppingCart);
        return "success";
    }

    public String deleteShoppingCart(ShoppingCart shoppingCart){
        if (shoppingCart.getGoodsId() == null||shoppingCart.getUserId() == null)
            return "fail";
        List<ShoppingCart> shoppingCartList = getShoppingCartList(shoppingCart.getUserId());

        for (ShoppingCart shoppingCart1 : shoppingCartList){
            if (shoppingCart1.getGoodsId().equals(shoppingCart.getGoodsId())){
                redisTemplate.opsForList().remove("shoppingCartList"+shoppingCart.getUserId(),0,shoppingCart1);
                return "success";
            }
        }
        return "success";
    }

//    public String updateShoppingCart(){
//
//        return "success";
//    }
}
